<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('project.work_date')" type="date" v-model="baTable.form.items!.work_date" prop="date" :placeholder="t('Please select field', { field: t('project.work_date') })" />
                    <FormItem :label="t('project.custom_name')" type="string" v-model="baTable.form.items!.custom_name" prop="custom_name" :placeholder="t('Please input field', { field: t('project.custom_name') })" />
                    <!-- <FormItem :label="t('project.creative_director')" type="string" v-model="baTable.form.items!.creative_director" prop="creative_director" :placeholder="t('Please input field', { field: t('project.creative_director') })" />
                    <FormItem :label="t('project.director')" type="string" v-model="baTable.form.items!.director" prop="director" :placeholder="t('Please input field', { field: t('project.director') })" />
                    <FormItem :label="t('project.dop')" type="string" v-model="baTable.form.items!.dop" prop="dop" :placeholder="t('Please input field', { field: t('project.dop') })" />
                    <FormItem :label="t('project.production_company')" type="string" v-model="baTable.form.items!.production_company" prop="production_company" :placeholder="t('Please input field', { field: t('project.production_company') })" />
                    <FormItem :label="t('project.executive_producer')" type="string" v-model="baTable.form.items!.executive_producer" prop="executive_producer" :placeholder="t('Please input field', { field: t('project.executive_producer') })" />
                    <FormItem :label="t('project.producer')" type="string" v-model="baTable.form.items!.producer" prop="producer" :placeholder="t('Please input field', { field: t('project.producer') })" />
                    <FormItem :label="t('project.tv_name')" type="string" v-model="baTable.form.items!.tv_name" prop="tv_name" :placeholder="t('Please input field', { field: t('project.tv_name') })" />
                    <FormItem :label="t('project.language')" type="string" v-model="baTable.form.items!.language" prop="language" :placeholder="t('Please input field', { field: t('project.language') })" />
                    <FormItem :label="t('project.prep_days')" type="string" v-model="baTable.form.items!.prep_days" prop="prep_days" :placeholder="t('Please input field', { field: t('project.prep_days') })" />
                    <FormItem :label="t('project.post_days')" type="string" v-model="baTable.form.items!.post_days" prop="post_days" :placeholder="t('Please input field', { field: t('project.post_days') })" />
                    <FormItem :label="t('project.studio_shooting_day')" type="string" v-model="baTable.form.items!.studio_shooting_day" prop="studio_shooting_day" :placeholder="t('Please input field', { field: t('project.studio_shooting_day') })" />
                    <FormItem :label="t('project.length')" type="string" v-model="baTable.form.items!.length" prop="length" :placeholder="t('Please input field', { field: t('project.length') })" /> -->
                    
                    <FormItem :label="t('project.title')" type="string" v-model="baTable.form.items!.title" prop="title" :placeholder="t('Please input field', { field: '年份+品牌+项目名称' })" />
                    <FormItem :label="t('项目经理')" type="remoteSelect" v-model="baTable.form.items!.manager_id" prop="manager_id" :input-attr="{ pk: 'ba_admin.id', field: 'nickname', 'remote-url': '/admin/auth.Admin/index' }" :placeholder="t('Please select field', { field: t('项目经理') })" />
                    <FormItem :label="t('project.member_ids')" type="remoteSelects" v-model="baTable.form.items!.member_ids" prop="member_ids" :input-attr="{ pk: 'ba_admin.id', field: 'nickname', 'remote-url': '/admin/auth.Admin/index' }" :placeholder="t('Please select field', { field: t('project.member_ids') })" />
                    <FormItem :label="t('project.custom_company')" type="string" v-model="baTable.form.items!.custom_company" prop="custom_company" :placeholder="t('Please input field', { field: t('project.custom_company') })" />
                    <FormItem :label="t('project.date')" type="date" v-model="baTable.form.items!.date" prop="date" :placeholder="t('Please select field', { field: t('project.date') })" />
                    <FormItem :label="t('project.tax')" type="number" v-model="baTable.form.items!.tax" prop="tax" :placeholder="t('Please input field', { field: t('project.tax') })" @change="taxChange"/>
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

watch(() => {
    return baTable.form.operate!
}, (val) => {
    if (val == 'Add') baTable.form.items!.tax = 0.06
})

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    title: [buildValidatorData({ name: 'required', title: t('project.title') })],
    manager_id: [buildValidatorData({ name: 'required', title: t('project.manager_id') })],
    member_ids: [buildValidatorData({ name: 'required', title: t('project.member_ids') })],
    custom_company: [buildValidatorData({ name: 'required', title: t('project.custom_company') })],
    custom_name: [buildValidatorData({ name: 'required', title: t('project.custom_name') })],
    creative_director: [buildValidatorData({ name: 'required', title: t('project.creative_director') })],

    date: [buildValidatorData({ name: 'required', title: t('project.date') })],
    sub_total: [buildValidatorData({ name: 'float', title: t('project.sub_total') })],
    tax: [
        buildValidatorData({ name: 'required', title: t('project.tax') })
    ],
    total: [buildValidatorData({ name: 'float', title: t('project.total') })],
})

const taxChange = (e: any) => {
    let val = Number(Number(e.target.value).toFixed(2))
    val = val < 0 ? 0 : val
    baTable.form.items!.tax = val
}

</script>

<style scoped lang="scss"></style>
